<?php
/**
 * Created by PhpStorm.
 * User: xsp
 * Date: 2018/4/22
 * Time: 19:39
 */

namespace common\models;


use yii\data\Pagination;
use yii\db\ActiveRecord;

class Category extends ActiveRecord
{
        public $title1;
        public function attributeLabels()
        {
            return [
                'parentid'=>'上级分类',
                'title1'=>'添加分类'
            ];
        }
        public function rules()
        {
         return [
           ['parentid','required','message'=>'上级目录不能为空'],
             ['title','required','message'=>'分类名称不能为空'],
            ['createtime','safe']
         ];
        
        }

    public function add($data)
        {
            $data['Category']['createtime'] = time();
            if($this->load($data)&&$this->save())
            {
                    return true;
            }

            return false;
        }

        public function getData()
        {
            return $cates = self::find()->asArray()->all();
        }

        public function getTree($cates,$pid = 0,$step=0)
        {
            $tree = [];
            foreach($cates as $cate) {
//                var_dump($cate);
                if($cate['parentid'] == $pid) {
                    $cate['title1'] = str_repeat("|==",$step). $cate['title'] ;
                    $tree[] = $cate;
                    $tree = array_merge($tree,$this->getTree($cates,$cate['cateid'],$step+1));
                }
            }
            return $tree;
        }


        public function getFix($datas)
        {
            $option[0] = "添加顶级分类";
            foreach ($datas as $data)
            {
                $option[$data['cateid']]=$data['title1'];
            }
            return $option;
        }
        public function getOption()
        {
            return $this->getFix($this->getTree($this->getData()));
        }
        public static function getMenu()
        {
            $cate = Category::find()->where(['parentid'=>0])->orderBy('createtime asc')->asArray()->all();
            $data = [];
            foreach ($cate as $key=>$value)
            {
                $value['children'] = Category::find()->where(['parentid'=>$value['cateid']])->asArray()->all();
                $data[$key]=$value;
            }
            return $data;
        }
        public function getPrimaryCate()
        {
           $data = self::find()->where(['parentid'=>0]);
           if (empty($data))
           {
               return [];
           }
           $pager = new Pagination(['totalCount'=>$data->count(),'pageSize'=>'10']);
           $data = $data->offset($pager->offset)->limit($pager->limit)->all();
           if (empty($data))
           {
               return [];
           }
           $primary = [];
           foreach ($data as $k =>$v)
           {
               $primary[]=[
                   'id'=>$v->cateid,
                   'text'=>$v->title,
                   'children'=>$this->getChildren($v->cateid)
               ];
           }
           return['data'=>$primary,'pages'=>$pager];
        }
        public function getChildren($id)
        {
            $data = self::find()->where(['parentid'=>$id])->all();
            if (empty($data)){
                return [];
            }
            $children = [];
            foreach ($data as $v)
            {
                $children [] = [
                    'id'=>$v->cateid,
                    'text'=>$v->title,
                    'children'=>$this->getChildren($v->cateid)
                ];
            }
            return $children;
        }

}